
package com.terren.spider.util.common;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.text.ParseException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.terren.spider.entity.core.DataType;
import com.terren.spider.entity.core.FieldType;
import com.terren.spider.entity.core.SpiderTask;

/**
 * @项目名称：报告系统
 * @类名称：StringUtil
 * @类描述：字符工具类 @创建人： @创建时间：2017-04-27 @修改人： @修改时间： @备注：
 */
public class StringUtil {
	
	
	private static int MYSQL_TINY_TEXT_LENGTH = (2 << 7) - 1;
    private static int MYSQL_TEXT_LENGTH = (2 << 15) - 1;
    private static int MYSQL_MEDIUM_TEXT_LENGTH = (2 << 23) - 1;
    private static int MYSQL_LONG_TEXT_LENGTH = (2 << 31) - 1;

    /*
     * 以UTF8编码格式截取字符串用于MySQL存储
     * UTF-8 规范参考  http://en.wikipedia.org/wiki/UTF-8
     */
    public static String truncate(String s, int maxBytes) {
        int b = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            int skip = 0;
            int width;
            if (c <= 0x007f) {
                width = 1;
            }
            else if (c <= 0x07FF) {
                width = 2;
            } else if (c <= 0xd7ff) {
                width = 3;
            } else if (c <= 0xDFFF) {
                width = 4;
                skip = 1;
            } else {
                width = 3;
            }
            if (b + width > maxBytes) {
                return s.substring(0, i);
            }
            b += width;
            i += skip;
        }
        return s;
    }

    
    public static int length(String s) {
    	if(null==s){
    		return 0;
    	}
        try {
            return s.getBytes("UTF-8").length;
        } catch (UnsupportedEncodingException e) {
            return 0;
        } catch (Exception e) {
            return 0;
        }
    }
    
    public static String truncateAsText(String s) {
    	if (length(s)>MYSQL_TEXT_LENGTH) {
    		return truncate(s, MYSQL_TEXT_LENGTH);
		}
        return s;
    }

    public static String truncateAsTinyText(String s) {
    	if (length(s)>MYSQL_TINY_TEXT_LENGTH) {
    		return truncate(s, MYSQL_TINY_TEXT_LENGTH);
		}
        return s;
    }

    public static String truncateAsMediumText(String s) {
    	if (length(s)>MYSQL_MEDIUM_TEXT_LENGTH) {
    		return truncate(s, MYSQL_MEDIUM_TEXT_LENGTH);
		}
        return s;
    }

    public static String truncateAsLongText(String s) {
    	if (length(s)>MYSQL_LONG_TEXT_LENGTH) {
    		return truncate(s, MYSQL_LONG_TEXT_LENGTH);
		}
        return s;
    }
	
	

	/**
	 * 去掉字符串中的空格
	 * 
	 * @param str
	 * @return
	 */
	public static String replaceBlank(String str) {
		String dest = "";
		if (str != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}

	/**
	 * 将带有统一中文全角空格的内容替换成换行符
	 * 
	 * @param sourContent
	 * @return
	 */
	public static String newLine(String sourContent) {

		String[] strs = sourContent.split("　");
		StringBuilder sb = new StringBuilder("");
		for (String str : strs) {
			if (!"".equals(str.trim()))
				sb.append("\\n\u3000\u3000" + StringUtil.replaceBlank(str));
		}
		return sb.toString();
	}

	/**
	 * 将带有统一空格的内容替换成换行符
	 * 
	 * @param sourContent
	 * @return
	 */
	public static String newLine2(String sourContent) {

		String[] strs = sourContent.split(" ");
		StringBuilder sb = new StringBuilder("");
		for (String str : strs) {
			if (!"".equals(str.trim()))
				sb.append("\\n\u3000\u3000" + StringUtil.replaceBlank(str));
		}
		return sb.toString();
	}

	/**
	 * 
	 * @方法功能描述: 对存在参数的输入内容进行替换操作，获无参数的完整字符串
	 * @方法名:getContent
	 * @param Element
	 *            classroot,List<Column> columns
	 * @return 无
	 * @throws 未知
	 * @参数描述 : String object
	 * @返回类型：String
	 * 
	 */
	public static String getContent(String content, Object parameter) {
		// 通过工具类获得装有格式化字段的map
		if (parameter != null) {
			Map<String, String> fieldValueMap = BeanUtil.getFieldValueMap(parameter);
			Set<String> keySet = fieldValueMap.keySet();
			for (String string : keySet) {
				if (StringUtils.contains(content, "${" + string + "}")) {
					content = StringUtils.replace(content, "${" + string + "}", fieldValueMap.get(string));
				}
			}
		}
		return content;
	}

	/**
	 * 判断一个字符串是否为数字字符串
	 */
	public static boolean isNum(String count) {
		Pattern pattern = Pattern.compile("[0-9]+.?[0-9]+");
		Matcher matcher = pattern.matcher((CharSequence) count);
		boolean flag = matcher.matches();
		return flag;
	}

	/**
	 * 把一个String类型的集合转换为Integer类型的集合
	 * 
	 * @param 1,2,3,4,5
	 * @param ,
	 * @return [1,2,3,4,5]
	 */
	public static List<Integer> getIntegerListByString(String str, String symbol) {
		if (str == null || str.trim().equals("")) {
			return null;
		}

		String[] sarr = str.split(symbol);
		Integer[] iarr = new Integer[sarr.length];
		for (int i = 0; i < sarr.length; i++) {
			iarr[i] = Integer.parseInt(sarr[i]);
		}
		return Arrays.asList(iarr);
	}

	/**
	 * 类型转换
	 */
	public static Object castString(String value, Class<?> cls) {
		String name = cls.getSimpleName();
		Object cast = value;
		if(cast != null){
			
			if (name.equalsIgnoreCase("Integer")) {
				Double castNum = getDoubleNum(cast.toString());
				cast =(int)Math.round(castNum);
			}
			if (name.equalsIgnoreCase("Long")) {
				Double castNum = getDoubleNum(cast.toString());
				cast =(Long)Math.round(castNum);
			}
			if (name.equalsIgnoreCase("Short")) {
				cast = Short.parseShort(cast.toString().trim());
			}
			if (name.equalsIgnoreCase("Float")) {
				cast = Float.parseFloat(cast.toString().trim());
			}
			if (name.equalsIgnoreCase("Double")) {
				cast = Double.parseDouble(cast.toString().trim());
			}
			if (name.equalsIgnoreCase("Boolean")) {
				cast = Boolean.parseBoolean(cast.toString().trim());
			}
		}
		return cast;
	}

	/**
	 * 字符串首字母大写
	 * 
	 * @param 字符串
	 * @return 首字母大写的字符串
	 */
	public static String toFirstUpperCase(String str) {
		if (str == null || str.length() < 1) {
			return "";
		}
		String start = str.substring(0, 1).toUpperCase();
		String end = str.substring(1, str.length());
		return start + end;
	}

	/**
	 * 转换为utf8格式
	 * 
	 * @param infile
	 * @param outfile
	 * @throws IOException
	 */
	public static void convertToUtf8(String infile, String outfile) throws IOException {

		java.io.File f = new java.io.File(infile);
		String type = GetCharset(f);
		// System.out.println("***********###############"+type);
		BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(infile), type));
		PrintWriter out = new PrintWriter(
				new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outfile), "utf-8")));
		String reading;
		while ((reading = in.readLine()) != null) {
			out.println(reading);
		}
		out.close();
		in.close();
	}

	/**
	 * 得到文件的字符集
	 * 
	 * @param file
	 * @return
	 */
	@SuppressWarnings("resource")
	public static String GetCharset(File file) {
		String charset = "GBK";
		byte[] first3Bytes = new byte[3];
		try {
			boolean checked = false;
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
			bis.mark(0);
			int read = bis.read(first3Bytes, 0, 3);
			if (read == -1)
				return charset;
			if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
				charset = "UTF-16LE";
				checked = true;
			} else if (first3Bytes[0] == (byte) 0xFE && first3Bytes[1] == (byte) 0xFF) {
				charset = "UTF-16BE";
				checked = true;
			} else if (first3Bytes[0] == (byte) 0xEF && first3Bytes[1] == (byte) 0xBB
					&& first3Bytes[2] == (byte) 0xBF) {
				charset = "UTF-8";
				checked = true;
			}
			bis.reset();
			if (!checked) {
				while ((read = bis.read()) != -1) {
					if (read >= 0xF0)
						break;
					if (0x80 <= read && read <= 0xBF)
						break;
					if (0xC0 <= read && read <= 0xDF) {
						read = bis.read();
						if (0x80 <= read && read <= 0xBF)
							continue;
						else
							break;
					} else if (0xE0 <= read && read <= 0xEF) {
						read = bis.read();
						if (0x80 <= read && read <= 0xBF) {
							read = bis.read();
							if (0x80 <= read && read <= 0xBF) {
								charset = "UTF-8";
								break;
							} else
								break;
						} else
							break;
					}
				}
			}

			bis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return charset;
	}

	/**
	 * 标题清理函数
	 */
	public static String titlecleaner(String str) {
		str = str.replaceAll("\n", "");
		str = str.replaceAll("\r", "");
		str = str.trim();
		String xnew = str.replaceAll("_新华网", "");
		int pos = xnew.lastIndexOf('_');
		if (pos != -1)
			xnew = xnew.substring(0, pos);
		return xnew.toLowerCase().trim();
	}

	/**
	 * 过滤XML
	 */
	public static String parValue(String value) {
		if (value == null) {
			return "";
		}
		value = value.replaceAll("&", "&amp;");
		value = value.replaceAll("<", "&lt;");
		value = value.replaceAll(">", "&gt;");
		value = value.replaceAll("\"", "&quot;");
		value = value.replaceAll("\'", "&apos;");
		return value;
	}

	/**
	 * 判断字符串中包含几个指定字符
	 */
	public static int countString(String src, String tar) {
		int count = 0;
		int offset = 0;
		while ((offset = src.indexOf(tar, offset)) != -1) {
			offset = offset + tar.length();
			count++;
		}
		return count;
	}

	/**
	 * 判断字符串包含的指定字符的最大index
	 */
	public static int maxIndex(String src, String tar) {
		int offset = 0;
		int index = 0;
		while ((offset = src.indexOf(tar, offset)) != -1) {
			index = offset;
			offset = offset + tar.length();
		}
		return index;
	}

	/**
	 * URL 解码
	 *
	 * @return String
	 * @author
	 * @date
	 */
	public static String getURLDecoderString(String str) {
		String result = "";
		if (null == str) {
			return "";
		}
		try {
			result = java.net.URLDecoder.decode(str, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * URL 转码
	 *
	 * @return String
	 * @author
	 * @date
	 */
	public static String getURLEncoderString(String str) {
		String result = "";
		if (null == str) {
			return "";
		}
		try {
			result = java.net.URLEncoder.encode(str, "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String formatSearchUrl(String keyword, String url) {
		String searchString = "${keyword}";
		return StringUtils.replace(url, searchString, keyword);
	}

	
	
	public static String formatSearchUrl(SpiderTask task,String url) {
		String[] searchList = new String[] {"${keyword}","${beginTime}","${endTime}","${site}"};
		
		for (String searchString : searchList) {
			if (StringUtils.contains(url, searchString)) {
				String propertyName = StringUtils.substringBetween(searchString, "${", "}");
				String replacement = (String)BeanUtil.getProperty(task, propertyName);
				url = StringUtils.replace(url, searchString, replacement);// 替换关键字
			}
		}
		return url;
	}
	
	
	/**
	 * 为入口url动态参数赋值
	 * 
	 * @param keyword
	 * @param startDay
	 * @param endDay
	 * @param url
	 * @return
	 * @throws ParseException
	 * @throws UnsupportedEncodingException
	 */
	/*public static String formatSearchUrl(SpiderTask tasks)
			throws ParseException, UnsupportedEncodingException {

		String returnUrl = "";
		String searchString = "${keyword}";
		String searchStartDay = "${startDay}";
		String searchEndDay = "${endDay}";
		String searchSite = "${site}";
		String charset;
		String encodeKey;
		String keyword = tasks.getKeyword();
		String site = tasks.getSite();
		String url = tasks.getEntryUrl();
		// 不同网站不同日期转换方式
		switch (tasks.getAct()) {
		case "baiduNews":// 百度新闻
			charset = "utf-8";
			keyword = getDealBaiduKeyWord(tasks.getKeyword());
			encodeKey = URLEncoder.encode(tasks.getKeyword(), charset);
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date stdate = formatter.parse(tasks.getBeginTime() + " 00:00:00");
			Date etdate = formatter.parse(tasks.getEndTime() + " 23:59:59");
			String st = stdate.getTime() / 1000 + "";
			String et = etdate.getTime() / 1000 + "";
			String url1 = StringUtils.replace(url, searchString, encodeKey);// 替换关键字
			String url2 = StringUtils.replace(url1, searchStartDay, st);// 替换开始时间
			String urlsite = StringUtils.replace(url2, searchEndDay, et);// 替换结束时间
			if(site==null){
				site="";
			}
			returnUrl = StringUtils.replace(urlsite, searchSite, site);// 替换站点
			break;
		case "baiduSearch":// 百度新闻
			charset = "utf-8";
			keyword = getDealBaiduKeyWord(keyword);
			encodeKey = URLEncoder.encode(keyword, charset);

			SimpleDateFormat formatter2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date stdate2 = formatter2.parse(tasks.getBeginTime() + " 00:00:00");
			Date etdate2 = formatter2.parse(tasks.getEndTime() + " 23:59:59");
			String st2 = stdate2.getTime() / 1000 + "";
			String et2 = etdate2.getTime() / 1000 + "";
			String url12 = StringUtils.replace(url, searchString, encodeKey);// 替换关键字
			String url22 = StringUtils.replace(url12, searchStartDay, st2);// 替换开始时间
			String urlsite2 = StringUtils.replace(url22, searchEndDay, et2);// 替换结束时间
			if(site==null){
				site="";
			}
			returnUrl = StringUtils.replace(urlsite2, searchSite, site);// 替换站点
			break;
		case "googleNews":// 谷歌新闻
			charset = "utf-8";
			keyword = getDealGoogleKeyWord(keyword);
			encodeKey = URLEncoder.encode(keyword, charset);
			String stDate = DateUtil.getTruncateDateTime2(tasks.getBeginTime());
			String etDate = DateUtil.getTruncateDateTime2(tasks.getEndTime());
			String url3 = StringUtils.replace(url, searchString, encodeKey);// 替换关键字
			String url4 = StringUtils.replace(url3, searchStartDay, stDate);// 替换开始时间
			returnUrl = StringUtils.replace(url4, searchEndDay, etDate);// 替换结束时间
			if(site==null){
				site="";
			}else{
				site="site:"+site;
			}
			returnUrl = StringUtils.replace(returnUrl, searchSite, site);// 替换站点
			break;
		case "sogouNews":
			charset = "gb2312";
			keyword = getDealBaiduKeyWord(keyword);
			encodeKey = URLEncoder.encode(keyword, charset);
			returnUrl = formatSearchUrl(encodeKey, url);
			if(site==null||"".equals(site)){
				site="";
			}else{
				site="site:"+site;
			}
			returnUrl = StringUtils.replace(returnUrl, searchSite, site);// 替换站点
			break;
		case "sogouSearch":
			charset = "gb2312";
			keyword = getDealBaiduKeyWord(keyword);
			encodeKey = URLEncoder.encode(keyword, charset);
			returnUrl = formatSearchUrl(encodeKey, url);
			if(site==null||"".equals(site)){
				site="";
			}else{
				site="site:"+site+"+";
			}
			returnUrl = StringUtils.replace(returnUrl, searchSite, site);// 替换站点
			break;
		case "sogouZhihu":
			charset = "utf-8";
			keyword = getDealBaiduKeyWord(keyword);
			encodeKey = URLEncoder.encode(keyword, charset);
			returnUrl = formatSearchUrl(encodeKey, url);
			
			break;
			//xiaohongshu
		case "xiaohongshu":
			charset = "utf-8";
			keyword = getDealBaiduKeyWord(keyword);
			encodeKey = URLEncoder.encode(keyword, charset);
			returnUrl = formatSearchUrl(encodeKey, url);
		default:
			charset = "utf-8";
			keyword = getDealBaiduKeyWord(keyword);
			encodeKey = URLEncoder.encode(keyword, charset);
			returnUrl = formatSearchUrl(encodeKey, url);
			break;

		}
		return returnUrl;
	}*/

	public static Boolean match(String str, String regex) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		if (matcher.matches()) {
			return true;
		}
		return false;
	}

	public static String getEncoding(String str) {
		String encode = "GB2312";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s = encode;
				return s;
			}
		} catch (Exception exception) {
		}
		encode = "ISO-8859-1";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s1 = encode;
				return s1;
			}
		} catch (Exception exception1) {
		}
		encode = "UTF-8";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s2 = encode;
				return s2;
			}
		} catch (Exception exception2) {
		}
		encode = "GBK";
		try {
			if (str.equals(new String(str.getBytes(encode), encode))) {
				String s3 = encode;
				return s3;
			}
		} catch (Exception exception3) {
		}
		return "";
	}

	public static boolean isWrongEncoding(String str) {
		return !java.nio.charset.Charset.forName("GBK").newEncoder().canEncode(str);
	}

	// 过滤特殊字符
	public static String StringFilter(String str) {
		// 只允许字母和数字
		// String regEx = "[^a-zA-Z0-9]";
		// 清除掉所有特殊字符
		String regEx = "[`~!@#$%^&*()+=|{}':;',//[//].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		return m.replaceAll("").trim();
	}

	// 过滤特殊字符 不包含“.”
	public static String StringFilterIgnoreDot(String str) {
		// 只允许字母和数字
		// String regEx = "[^a-zA-Z0-9]";
		// 清除掉所有特殊字符
		String regEx = "[`~!@#$%^&*()+=|{}':;',//[//]<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		return m.replaceAll("").trim();
	}

	/**
	 * 从字符串 截取 并转换成整型
	 * 
	 * @param str
	 * @param formatter
	 * @return
	 */
	public static Integer formatInt(String str, String formatter) {
		String replace = DataType.INTEGER.getTypeReplace();// ${d}
		String result = formatString(str, formatter, replace);
		return result != null ? Integer.parseInt(StringFilter(result)) : null;
	}

	/**
	 * 从字符串 截取 并转换成double型
	 * 
	 * @param str
	 * @param formatter
	 * @return
	 */
	public static Double formatDouble(String str, String formatter, String replace) {
		
		String result = formatString(str, formatter, replace);
		return result != null ? Double.parseDouble(StringFilterIgnoreDot(result)) : null;
	}


	/**
	 * 从字符串 截取
	 * 
	 * @param str
	 * @param formatter
	 * @return
	 */
	public static String formatString(String str, String formatter, String replace) {
		replace = "{"+replace+"}";
		String result = null;
		if (StringUtils.contains(formatter, replace)) {
			String begin = StringUtils.substringBefore(formatter, replace);
			String end = StringUtils.substringAfter(formatter, replace);
			
			if (begin.equals("") && end.equals("")) {
				result = str;
			} else {
				if (!end.equals("")) {
					result = StringUtils.substringBetween(str, begin, end);
				} else {
					result = StringUtils.substringAfter(str, begin);
				}
			}
		}
		
		return result;
	}
	/**
	 * 从字符串 截取{}部分内容
	 * 
	 * @param str
	 * @param formatter
	 * @return
	 */
	public static String formatTimeFor(String formatter) {
		String result = formatter;
		if (formatter.contains("{")) {
			result = StringUtils.substringBetween(formatter, "{", "}");	
		}
		
		return result;
	}
	
	/**
	 * 
	 * @param str 未处理的字符串                       [编辑作者：李秀荣]
	 * @param formatter  处理的格式规范  [编辑作者：{author}]
	 * @param replaceName          [author]
	 * @param fieldType            [String]
	 * @return
	 */
	public static Object format(String str, String formatter,String replaceName, FieldType fieldType,String language) {
		if (formatter != null && !"".equals(formatter)) {

			switch (fieldType) {
			case STRING:
				return formatString(str, formatter,replaceName);
			case DOUBLE:
				return formatDouble(str, formatter,replaceName);
			case DATE:
				return DateUtil.formatDateString(str, formatter,language);
			default:
				return formatString(str, formatter,replaceName);
			}
		}
		return str;
	}
	public static String addGroupRegex(String regexUrls,String split) {
		String regex = null;
		if (null!=regexUrls) {
			regex = "";
			String [] regexs = regexUrls.split(split);
			for (int index = 0;index<regexs.length;index++) {
				if (index == regexs.length - 1) {
					regex += "("+regexs[index]+")";
				}
				else {
					regex += "("+regexs[index]+")，";
				}
				
			}
		}
		return regex;
	}
	/**
	 * 获取字符串中的数字
	 * @param content
	 * @return
	 */
	public static String getNumbers(String content) {
		Pattern pattern = Pattern.compile("\\d+");
		Matcher matcher = pattern.matcher(content);
		while (matcher.find()) {
			return matcher.group(0);
		}
		return "";
	}
	/**
	 * 去掉字符串中匹配到正则的部分
	 * @param str
	 * @param regex
	 * @return
	 */
	public static String getReplaceStr(String str,String regex){
		String result = "";
		if(!"".equals(str)&&str!=null){
			Pattern patternForProtocal = Pattern.compile(regex);
			result = patternForProtocal.matcher(str).replaceAll("");
		}
		return result;
	}
	/**
	 * 处理百度搜索中有逻辑条件的关键词
	 * @param oldStr
	 * @return
	 */
	public static String getDealBaiduKeyWord(String oldStr){
		String result = "";
		if(!"".equals(oldStr) && oldStr !=null){
			result = oldStr.replaceAll("&", " ");//&替换为空格
			result = result.replaceAll("!", " -");//！替换为 -
		}
		return result;
	}
	
	/**
	 * 处理Google 搜索中有逻辑条件的关键词
	 * @param oldStr
	 * @return
	 */
	public static String getDealGoogleKeyWord(String oldStr){
		String result = "";
		if(!"".equals(oldStr) && oldStr !=null){
			result = oldStr.replaceAll("&", " ");//&替换为空格
			result = result.replaceAll("\\|", "OR");//|替换为OR
			result = result.replaceAll("!", " -");//！替换为 -
		}
		return result;
	}
	/**
	 * 替换
	 * @param str
	 * @param format
	 * @return
	 */
	public static String messageFormat(String format,Object[] args) {
		MessageFormat messageFormat = new MessageFormat(format);
		return messageFormat.format(args);
	}
	
	/**
	 * 通过正则获取字符串中的需要内容
	 * @param sourceStr 需要解析的字符串
	 * @param regex 需要匹配的字符串正则
	 * @return 返回正则匹配的字符串
	 */
	public static String regexStr(String sourceStr,String regexStr){
		StringBuilder returnStr = new StringBuilder();
		Pattern regex = Pattern.compile(regexStr, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
		int group = -1;
		Matcher matcher = regex.matcher(sourceStr);
		while (matcher.find()) {
			String[] groups = new String[matcher.groupCount() + 1];
			for (int i = 0; i < groups.length; i++) {
				groups[i] = matcher.group(i);
			}
			if (regex.matcher("").groupCount() == 0) {
				group = 0;
			} else {
				group = 1;
			}
			returnStr.append(groups[group]);
		}
		return returnStr.toString();
	}
	/**
	 * 替换字符串中的特殊字符
	 * @param sourceStr 原始字符串
	 * @param replaceStr 特殊字符的替换符
	 * @return
	 */
	public static String specialFilter(String sourceStr,String replaceStr) {
	    String regEx = "[`~!@#$%^&*()\\-+={}':;,\\[\\].<>/?￥%…（）_+|【】‘；：”“’。，、？\\s]";
	    Pattern p = Pattern.compile(regEx);
	    Matcher m = p.matcher(sourceStr);
	    return m.replaceAll(replaceStr).trim();
	}
	/**
	 * 解析HTML中的作者，新闻来源
	 * @param html
	 * @param keyStr
	 * @return
	 */
	public static String getParseStr(String html, String keyStr) {
		if (null == html || null == keyStr) {
			return null;
		}
		String parseStr = "";
		if (html.contains(keyStr)) {
			String authorStr = StringUtils.substringAfter(html, keyStr);
			String testStr = authorStr.substring(0, 3).trim();
			if (testStr.contains("<")) {
				authorStr = authorStr.substring(0, 200).trim();
				// 匹配字符串里面的所有汉字
				parseStr = StringUtil.regexStr(authorStr, "[\u4E00-\u9FA5]+");

			} else {
				authorStr = authorStr.substring(0, 20).trim();
				// 将特殊字符替换成空格
				authorStr = StringUtil.specialFilter(authorStr, " ");
				parseStr = StringUtils.substringBefore(authorStr, " ");
			}

		}

		return parseStr;
	}
	/**
	 * 对数字字符串中含有“K”，“，”，“千”，“万”的情况进行处理
	 * @param castStr
	 * @return
	 */
	public static Double getDoubleNum(String castStr){
		castStr = replaceBlank(castStr);
		if(castStr==null ||"".equals(castStr)){
			return null;
		}
		Double cast=0.0;
		
		try {
			if(castStr.contains("K")){
				castStr = castStr.replace("K", "");
				cast = Double.parseDouble(castStr)*1000;	
			}else if(castStr.contains(",")){
				castStr = castStr.replace(",", "");
				cast = Double.parseDouble(castStr);	
			}else if(castStr.contains("千")){
				castStr = castStr.replace("千", "");
				cast = Double.parseDouble(castStr)*1000;
			}else if(castStr.contains("万")){
				castStr = castStr.replace("万", "");
				cast = Double.parseDouble(castStr)*10000;
			}else{
				castStr = specialFilter(castStr,"");
				cast = Double.parseDouble(castStr);
			
			}
		} catch (NumberFormatException e) {
			
			e.printStackTrace();
			return 0.0;
		}
		return cast;
	}
	public static int getWordOccurance(String content,String keyword) {
		int occurance = 0;
		keyword = StringUtils.replace(keyword, "(", "");
		keyword = StringUtils.replace(keyword, ")", "");
		String[] keys = keyword.split("&");
		for (String string : keys) {
			occurance = occurance + org.springframework.util.StringUtils.countOccurrencesOf(content, string);
		}
		return occurance;
	}
	/**
	 * 只编码url里的汉字
	 * @param str
	 * @param charset
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String encodeZH(String str, String charset){
		String zhPattern = "^[\\u4e00-\\u9fa5]*$";
	    Pattern p = Pattern.compile(zhPattern);
	    StringBuffer b = new StringBuffer();
	    char[] ch = str.toCharArray();
	    for(char charStr : ch){
	    	 Matcher m = p.matcher(String.valueOf(charStr));
	    	 while (m.find()) {
	   	      try {
				m.appendReplacement(b, URLEncoder.encode(m.group(0), charset));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
	   	    }
	   	    m.appendTail(b);
	    }
	    return b.toString();
	  }
}